//
//  YHRetweetView.swift
//  XLWB
//
//  Created by key on 16/6/29.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

class YHRetweetView: UIView {

    private var con: Constraint?
    
    //MARK: - 数据加载
    var status: YHHomeList? {
        
        didSet{
            
            contentLabel.attributedText = status?.retweeted_status?.textAttr
            
            
            //配图数据
            //判断
            
            con?.uninstall()
            
            if status?.pic_urls?.count <= 0 {
                
                //没有配图信息
                pickImgView.hidden = true
                
                //刷新约束
                //约束自身的底边
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    con = make.bottom.equalTo(contentLabel).offset(spac).constraint
                    
                })
                
            }else{
                //有配图数据
                pickImgView.hidden = false
                
                //传递数据
                pickImgView.pictureInfo = status?.retweeted_status?.pic_urls
                
                //刷新约束
                //约束自身的底边
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    con = make.bottom.equalTo(pickImgView).offset(spac).constraint
                    
                })
                
                
                
            }
            
        }
        
        
    }
    
    
    
    //MARK: - 懒加载子控件
    //转发微博的文字内容
    private lazy var contentLabel: UILabel = {
        
        let content = UILabel(textFont: 15, textColor: UIColor.blackColor(), maxLabelW: screenW - spac*2)
        
        return content
        
    }()
        
    //原创微博图片
    private lazy var pickImgView: YHPickImgView = {
        
        let pickImg = YHPickImgView()
        
        return pickImg
        
    }()
    
    
    //MARK: - 入口
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //添加子控件
        addChildrenView()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 添加子控件
    func addChildrenView() {
        
        addSubview(contentLabel)
        addSubview(pickImgView)
        
        //约束子控件
        setSubViewFrame()
        
        
    }
    
    //MARK: - 约束子控件
    func setSubViewFrame() {
        
        //转发微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.equalTo(self).offset(spac)
            
        }
        
        
        //微博 图片
        pickImgView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(spac)
            make.leading.equalTo(contentLabel)
        }
        
        //自身
        self.snp_makeConstraints { (make) -> Void in
            
            con = make.bottom.equalTo(pickImgView).offset(spac).constraint
            
        }
        
        
        
        
    }
    

}
